package com.lq.order.controller;

import com.lq.order.entity.OrderItem;
import com.lq.order.service.IOrderItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 订单项表 前端控制器
 * </p>
 *
 * @author lq
 * @since 2025-08-22
 */
@RestController
@RequestMapping("/order-items")
@Api(tags = "订单项管理", description = "订单项相关接口")
public class OrderItemController {

    @Autowired
    private IOrderItemService orderItemService;

    @ApiOperation(value = "创建订单项", notes = "创建一个新的订单项")
    @ApiResponses({
            @ApiResponse(code = 200, message = "创建成功"),
            @ApiResponse(code = 400, message = "请求参数错误"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    @PostMapping("/")
    public OrderItem createOrderItem(@ApiParam(value = "订单项信息", required = true) @RequestBody OrderItem orderItem) {
        orderItemService.save(orderItem);
        return orderItem;
    }

    @ApiOperation(value = "根据ID获取订单项", notes = "根据订单项ID获取订单项详细信息")
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 404, message = "订单项不存在"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    @GetMapping("/{id}")
    public OrderItem getOrderItemById(@ApiParam(value = "订单项ID", required = true) @PathVariable Long id) {
        return orderItemService.getById(id);
    }

    @ApiOperation(value = "根据订单ID获取订单项列表", notes = "根据订单ID获取所有相关的订单项")
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    @GetMapping("/order/{orderId}")
    public List<OrderItem> getOrderItemsByOrderId(@ApiParam(value = "订单ID", required = true) @PathVariable Long orderId) {
        return orderItemService.getOrderItemsByOrderId(orderId);
    }

    @ApiOperation(value = "获取所有订单项", notes = "获取系统中所有的订单项")
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    @GetMapping("/")
    public List<OrderItem> getAllOrderItems() {
        return orderItemService.list();
    }

    @ApiOperation(value = "更新订单项", notes = "根据订单项ID更新订单项信息")
    @ApiResponses({
            @ApiResponse(code = 200, message = "更新成功"),
            @ApiResponse(code = 400, message = "请求参数错误"),
            @ApiResponse(code = 404, message = "订单项不存在"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    @PutMapping("/{id}")
    public OrderItem updateOrderItem(@ApiParam(value = "订单项ID", required = true) @PathVariable Long id,
                             @ApiParam(value = "订单项信息", required = true) @RequestBody OrderItem orderItem) {
        orderItem.setId(id);
        orderItemService.updateById(orderItem);
        return orderItem;
    }

    @ApiOperation(value = "删除订单项", notes = "根据订单项ID删除订单项")
    @ApiResponses({
            @ApiResponse(code = 200, message = "删除成功"),
            @ApiResponse(code = 404, message = "订单项不存在"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    @DeleteMapping("/{id}")
    public void deleteOrderItem(@ApiParam(value = "订单项ID", required = true) @PathVariable Long id) {
        orderItemService.removeById(id);
    }
}